package com.dd.stack.se.stream.concurrent;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/9/4 18:56
 * @Description 类功能作用说明
 */
public class SynchronizedExample {
    private boolean running = true;
    public synchronized void run() {
        while (!running) {
            // 空循环
        }
        System.out.println("线程停止");
    }
    public synchronized void stop() throws Exception {
        running = false;
        System.out.println("running = false");
        Thread.sleep(20);
    }

    public static void main(String[] args) throws Exception {
        SynchronizedExample example = new SynchronizedExample();
        Thread t = new Thread(example::run);
        t.start();

        Thread.sleep(1000); // 确保线程已经启动
        example.stop();
        t.join(); // 等待线程结束
    }
}
